class AutomatedUpdatorController < ApplicationController
  
  layout 'gcal'
  
  $main_url = "http://atelier.inf.unisi.ch:8000/gcal"
  $time_to_connect = " 01:00:00 "
  $break_point_html = "<body>"
  $sleeping_time = 600
  @@status = "stopped"
  @@running_offset = 0


  #Solution to be able to connect to the Google Calendar everyday at 1AM
  #We created a thread where kepps running constantly calling the index of GCAL controller through an HTTP request.
  def index

    if @@status != "running"
      puts "=> Index AutomatedUpdator Thread Started!"
      
     @@thread = Thread.new {
        @@status = "running"
        
        while true
          sleep(@@running_offset)
          puts "============== Connecting to #{$main_url}! ================="
        
#          url = "http://0.0.0.0:3000/gcal?flag=1"
#          result = Net::HTTP.get(URI(url))
#           @doc = REXML::Document.new result
#           puts "=================================================== "+@doc
#          puts result
          
#          resonse_array = result.split("<body>")
#          class_room_result = resonse_array[1].split("\n")[3]
#          seminar_room_result = resonse_array[1].split("\n")[4]
          responses_from_gcal = contact_class_and_seminars
          manage_responses(responses_from_gcal)
         
          time1 = Time.now
          time2 = time1+(60*60*24)
          next_time_to_query = Time.parse(time2.strftime("%a %b %d")+$time_to_connect+time2.strftime("%Z %Y"))
          @@running_offset = next_time_to_query.to_i - time1.to_i
        
          puts "=> Next read from the calendar will be in: "+@@running_offset.to_s+" seconds"
          puts "=> Sleeping until next time to connect"
          puts
        end        

        }
    end
    @status = @@status
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @automated_updator}
    end
 end

  #Here we just manage the responses.
  #We check if the rsposnes were false or true for class_rooms and Seminars.
  #Another check would be if the classes or Seminars are empty.  
  def manage_responses responses
    class_response = responses[0]
    seminar_response = responses[1]
    if(class_response == "true" && seminar_response == "true") then return true end
    if(class_response == "false" && seminar_response == "false")
     puts "================================= I'll SLEEP BAD ====================================="
      sleep($sleeping_time)
      return manage_responses(contact_class_and_seminars)
    elsif(class_response == "false") 
     puts "================================= I'll SLEEP CLASS ====================================="
       while(contact_class == "false") do sleep($sleeping_time) end
       return true
    elsif(seminar_response == "false")
    puts "================================= I'll SLEEP SEMINAR ====================================="
      while(contact_seminars == "false") do sleep($sleeping_time) end
      return true
    end
  end
 
 
  #Just contacts Gcal index sending as parameter to fetch Everything
  def contact_class_and_seminars
    url = $main_url+"?flag=All"
    result = Net::HTTP.get(URI(url))
    resonse_array = result.split($break_point_html)
    class_room_result = resonse_array[1].split("\n")[3]
    seminar_room_result = resonse_array[1].split("\n")[4]
    return[class_room_result, seminar_room_result]
  end
    
  #Just contacts Gcal index sending as parameter to fetch Class_rooms    
  def contact_class 
    url = $main_url+"?flag=Class"
    result = Net::HTTP.get(URI(url))
    resonse_array = result.split($break_point_html)
    return resonse_array[1].split("\n")[3]       
  end
        
  #Just contacts Gcal index sending as parameter to fetch Seminars     
  def contact_seminars
    url = $main_url+"?flag=Seminar"
    result = Net::HTTP.get(URI(url))
    resonse_array = result.split($break_point_html)
    return resonse_array[1].split("\n")[3]   
  end
 
end

